﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WeiXinMongoApis.Common;
using WeiXinMongoApis.Models;

namespace WeiXinMongoApis.Controllers
{
    public class ReportController : Controller
    {
        const string TrialUsersFileName = "TrialUsers.txt";

        List<TrialUser> _trialUsers;
        List<TrialUser> TrialUsers
        {
            get
            {
                if (_trialUsers != null)
                {
                    return _trialUsers;
                }
                _trialUsers = this.GetTrialUsers();
                return _trialUsers;
            }
        }

        // GET: Report
        public ActionResult Index()
        {
            ViewBag.LastdayTrialUsersCount = this.GetTrialUsersByAddday(-1).Count();
            ViewBag.TodayTrialUsersCount = this.GetTrialUsersByAddday(0).Count();
            ViewBag.UsersCount = this.GetTrialUsersCount();
            ViewBag.MinDate = this.GetMinTrialDate();
            ViewBag.MaxDate = this.GetMaxTrialDate();
            ViewBag.UsersList = this.GetTrialUsersGroupByMonth();
            ViewBag.LoginCountAll = this.GetLoginLogUsers().Count;
            ViewBag.LastdayLoginCount = this.GetLoginLogUsers().Where(r => r.LoginTime.Date == DateTime.Now.Date.AddDays(-1)).Count();
            ViewBag.TodayLoginCount = this.GetLoginLogUsers().Where(r => r.LoginTime.Date == DateTime.Now.Date).Count();
            ViewBag.LoginUsers = this.GetLoginLogUsers();
            return View();
        }

        private List<TrialUser> GetTrialUsers()
        {
            List<TrialUser> users = new List<TrialUser>();
            var trialUsersFileRows = FileHelperFunc.ReadFrom(
                Path.Combine(AppDomain.CurrentDomain.BaseDirectory, TrialUsersFileName)
                );
            foreach (var item in trialUsersFileRows)
            {
                var userArr = item.Split(',');
                if (userArr.Length >= 2)
                {
                    string ipAddress = userArr[0];
                    DateTime startDate;
                    if (DateTime.TryParse(userArr[1].ToString(), out startDate) == false)
                    {
                        continue;
                    }
                    users.Add(new TrialUser()
                    {
                        IpAddress = ipAddress,
                        StartDate = startDate
                    });
                }
            }
            return users;
        }

        private List<LoginUser> GetLoginLogUsers()
        {
            List<LoginUser> users = new List<LoginUser>();
            System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Log"));
            FileInfo[] loginLogFiles = dir.GetFiles();
            foreach (var file in loginLogFiles)
            {
                var lines = FileHelperFunc.ReadFrom(file.FullName);
                foreach (var line in lines)
                {
                    var userArr = line.Split(',');
                    if (userArr.Length >= 3)
                    {
                        LoginUser loginUser = new LoginUser();
                        //客户端识别码
                        loginUser.ClientId = userArr[0];
                        //登录时间
                        DateTime startDate;
                        if (DateTime.TryParse(userArr[1].ToString(), out startDate) == false)
                        {
                            continue;
                        }
                        loginUser.LoginTime = startDate;
                        //IP地址
                        loginUser.IpAddress = userArr[2];
                        if (userArr.Length >= 4)
                        {
                            var winVersion = userArr[3];
                            string displayVersion = "未获取";
                            if (winVersion.Contains("Microsoft Windows NT 5.1"))
                            {
                                displayVersion = "Win XP";
                            }
                            else if (winVersion.Contains("Microsoft Windows NT 6.0"))
                            {
                                displayVersion = "Windows Vista";
                            }
                            else if (winVersion.Contains("Microsoft Windows NT 6.1"))
                            {
                                displayVersion = "Win7";
                            }
                            else if (winVersion.Contains("Microsoft Windows NT 6.2"))
                            {
                                displayVersion = "Win8";
                            }
                            else if (winVersion.Contains("Microsoft Windows NT 6.3"))
                            {
                                displayVersion = "Win8.1";
                            }
                            else if (winVersion.Contains("Microsoft Windows NT 6.4"))
                            {
                                displayVersion = "Win10";
                            }
                            else if (winVersion.Contains("Microsoft Windows NT 10"))
                            {
                                displayVersion = "Win10";
                            }
                            //操作系统版本
                            loginUser.OsVersion = displayVersion;
                        }
                        if (userArr.Length >= 5)
                        {
                            //应用程序当前目录
                            loginUser.CurrentDirectory = userArr[4];
                        }
                        if (userArr.Length >= 6)
                        {
                            //机器名
                            loginUser.MachineName = userArr[5];
                        } 
                        if (userArr.Length >= 7)
                        {
                            //当前登录用户
                            loginUser.UserName = userArr[6];
                        }
                        if (userArr.Length >= 8)
                        {
                            //Mac地址
                            loginUser.MacAddress = userArr[7];
                        }
                        if (userArr.Length >= 9)
                        {
                            //硬盘ID
                            loginUser.HardID = userArr[8];
                        }
                        if (userArr.Length >= 10)
                        {
                            //主板ID
                            loginUser.Motherboard = userArr[9];
                        }

                        users.Add(loginUser);
                    }
                }
            }
            return users;
        }

        private int GetTrialUsersCount()
        {
            var users = this.GetTrialUsers();
            return TrialUsers.Count;
        }

        private IEnumerable<TrialUser> GetTrialUsersByAddday(int days)
        {
            return TrialUsers.Where(r => r.StartDate.Date == DateTime.Now.Date.AddDays(days));
        }

        private DateTime GetMinTrialDate()
        {
            return TrialUsers.Min(r => r.StartDate);
        }

        private DateTime GetMaxTrialDate()
        {
            return TrialUsers.Max(r => r.StartDate);
        }

        private List<List<TrialUser>> GetTrialUsersGroupByMonth()
        {
            List<List<TrialUser>> usersList = new List<List<TrialUser>>();

            DateTime start = GetMinTrialDate();
            DateTime end = GetMaxTrialDate();
            DateTime tempYmd = start;

            while (new DateTime(tempYmd.Year, tempYmd.Month, 1) <= new DateTime(end.Year, end.Month, 1))
            {
                List<TrialUser> users = new List<TrialUser>();
                var targetMonthUsers = TrialUsers.Where(r => r.StartDate.Year == tempYmd.Year && r.StartDate.Month == tempYmd.Month);
                foreach (var item in targetMonthUsers)
                {
                    users.Add(item);
                }
                usersList.Add(users);
                tempYmd = tempYmd.AddMonths(1);
            }
            return usersList;
        }

    }
    public class TrialUser
    {
        public string IpAddress { get; set; }
        public DateTime StartDate { get; set; }
    }
    public class LoginUser
    {
        //客户端识别码
        public string ClientId { get; set; }
        //登录时间
        public DateTime LoginTime { get; set; }
        //IP地址
        public string IpAddress { get; set; }
        //操作系统版本
        public string OsVersion = "";
        //应用程序当前目录
        public string CurrentDirectory = "";
        //机器名
        public string MachineName = "";
        //当前登录用户
        public string UserName = "";
        //Mac地址
        public string MacAddress = "";
        //硬盘ID
        public string HardID = "";
        //主板ID
        public string Motherboard = "";
    }
}